package com.maimang.platform.quartz.controller;

import com.maimang.platform.annotation.Log;
import com.maimang.platform.base.BaseController;
import com.maimang.platform.base.Result;
import com.maimang.platform.common.utils.ExcelUtil;
import com.maimang.platform.enums.BusinessType;
import com.maimang.platform.page.TableDataInfo;
import com.maimang.platform.quartz.entity.SysJobLog;
import com.maimang.platform.quartz.service.SysJobLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Rocky
 * @version 1.0.0
 * @classname SysJobLogController
 * @description 调度日志操作处理
 * @date 2019-04-16 23:47
 * @email jintaozhao@qq.com
 */
@Controller
@RequestMapping("/monitor/jobLog")
@Api(value = "任务日志", description = "定时任务执行日志操作")
public class SysJobLogController extends BaseController {

	private String prefix = "monitor/job";

	@Autowired
	private SysJobLogService jobLogService;


	@RequiresPermissions("monitor:job:view")
	@GetMapping
	public String jobLog() {
		return prefix + "/jobLog";
	}

	@RequiresPermissions("monitor:job:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(SysJobLog jobLog) {
		startPage();
		List<SysJobLog> list = jobLogService.selectJobLogList(jobLog);
		return getDataTable(list);
	}

	@ApiOperation(value = "导出日志", notes = "将quartz任务执行日志导出到Excel中")
	@Log(title = "调度日志", businessType = BusinessType.EXPORT)
	@RequiresPermissions("monitor:job:export")
	@PostMapping("/export")
	@ResponseBody
	public Result export(SysJobLog jobLog) {
		List<SysJobLog> list = jobLogService.selectJobLogList(jobLog);
		ExcelUtil<SysJobLog> util = new ExcelUtil<SysJobLog>(SysJobLog.class);
		return util.exportExcel(list, "调度日志");
	}

	@Log(title = "调度日志", businessType = BusinessType.DELETE)
	@RequiresPermissions("monitor:job:remove")
	@PostMapping("/remove")
	@ResponseBody
	public Result remove(String ids) {
		return toResult(jobLogService.deleteJobLogByIds(ids));
	}

	@RequiresPermissions("monitor:job:detail")
	@GetMapping("/detail/{jobLogId}")
	public String detail(@PathVariable("jobLogId") Long jobLogId, ModelMap modelMap) {
		modelMap.put("name", "jobLog");
		modelMap.put("jobLog", jobLogService.selectJobLogById(jobLogId));
		return prefix + "/detail";
	}

	@Log(title = "调度日志", businessType = BusinessType.CLEAN)
	@RequiresPermissions("monitor:job:remove")
	@PostMapping("/clean")
	@ResponseBody
	public Result clean() {
		jobLogService.cleanJobLog();
		return success();
	}
}
